<?php
/*
	[DESTOON B2B System] Copyright (c) 2008-2018 www.destoon.com
	This is NOT a freeware, use is subject to license.txt
*/
defined('IN_DESTOON') or exit('Access Denied');
class dsession {
	var $obj;

    function __construct() {
		$this->obj = new Redis;
		include DT_ROOT.'/file/config/redis.inc.php';
		$num = count($RedisServer);
		$key = $num == 1 ? 0 : abs(crc32(DT_IP))%$num;
		$this->obj->connect($RedisServer[$key]['host'], $RedisServer[$key]['port']);
        // --  @# fulltimelink @ 2018-08-10 15:27:24
        // --  @# redis连接添加密码选项，暂不放到配置项
        $this->obj->auth("Hnr@redis2018");

		if(DT_DOMAIN) @ini_set('session.cookie_domain', '.'.DT_DOMAIN);
    	session_set_save_handler(array(&$this,'open'), array(&$this,'close'), array(&$this,'read'), array(&$this,'write'), array(&$this,'destroy'), array(&$this,'gc'));
		session_cache_limiter('private, must-revalidate');
		session_start();
		header("cache-control: private");
    }

    function dsession() {
		$this->__construct();
    }

	function open($path, $name) {
		return true;
	}

	function close() {
		return true;
	}

	function read($sid) {
        // --  @# fulltimelink @ 2018-08-11 13:16:17
        // --  @# php7下 保证redis每次都会返回字符串（false时返回空字符串），否则会引起sesstion_start启动不了报错
        // --  @# https://stackoverflow.com/questions/41540302/failed-to-read-session-data-on-php7-1
		//return $this->obj->get($sid);
        return $this->obj->get($sid)?:"";
	}

	// --  @# annotated by fulltimelink @ 2018-09-18 18:17:31
	// --  @# 默认session有效期保存1800s   更改为43200s
	function write($sid, $data) {
		//return $this->obj->setex($sid, 1800, $data);
		return $this->obj->setex($sid, 43200, $data);
	}

	function destroy($sid) {
	     return $this->obj->delete($sid);
	}

	function gc() {
	    return true;
	}
}
?>